//
//  MBHouseBTNCell.m
//  发布求租
//
//  Created by MB__Lee on 2018/6/9.
//  Copyright © 2018年 MB__Lee. All rights reserved.
//

#import "MBHouseBTNCell.h"
#import "MBCircleBTN.h"

@interface MBHouseBTNCell()

@property (weak, nonatomic) IBOutlet UILabel *xingLabel;

@property (weak, nonatomic) IBOutlet UILabel *nameLabel;



@property(nonatomic,strong)MBCircleBTN *lastBTN;
@end

@implementation MBHouseBTNCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.xingLabel.hidden = YES;
    
    
}

-(void)setBTNModel:(MBCircleBTNModel *)BTNModel{
    [super setBTNModel:BTNModel];
    self.nameLabel.text = BTNModel.title;
    // *  是否隐藏
    if (BTNModel.hasX) {
        self.xingLabel.hidden = NO;
    }else{
        self.xingLabel.hidden = YES;
    }
    
    // 2.生成按钮
    CGFloat margin = 10;
    
    CGFloat hight = 32;
    
    
    for (UIView *btn in self.subviews) {
        if ([btn isKindOfClass:[MBCircleBTN class]]) {
            [btn removeFromSuperview];
        }
    }
    
    
    MBCircleBTN *tempBtn = [[MBCircleBTN alloc] initWithFrame:CGRectMake(0, 40, 0, 0)];
    

    
    for (int i = 0; i< BTNModel.btnNameMut.count; i++) {
        //计算frame
        CGFloat x = CGRectGetMaxX(tempBtn.frame);
        x += margin;
        
        CGFloat width = [self caculateStringWidth:BTNModel.btnNameMut[i]];
        
        CGFloat allwith = x + width;
        
         CGFloat y = tempBtn.frame.origin.y;
        
        if (allwith + margin > SCREEN_WIDTH) {
            x = margin;
            
            y =  CGRectGetMaxY(tempBtn.frame)+ margin;
        }
        
        
        MBCircleBTN *btn = [MBCircleBTN buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(x, y, width, hight);
        [btn setTitle:BTNModel.btnNameMut[i] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(backBTN:) forControlEvents:UIControlEventTouchUpInside];
       
        btn.layer.cornerRadius = 0.5 *hight;
        
        [self addSubview:btn];
        
        btn.tag = 100+ i;
        
        tempBtn = btn;
        
    
    }
    
    
}
-(void)backBTN:(MBCircleBTN *)btn{
    _lastBTN.selected = NO;
    
    _lastBTN = btn;
    btn.selected = YES;
    
    [self dealWithTag:btn.tag-100];
    
}

-(void)dealWithTag:(NSUInteger)tag{
    if (self.type == BaseTypeDecoration) {

        self.BTNModel.decoration = self.BTNModel.btnNameMut[tag];
        
        
        
    }else if (self.type == BaseTypeRentMethod){
        
        
        
        NSArray *arr  = @[@"1",@"3",@"6",@"12"];
        self.BTNModel.rent_method = arr[tag];
        
        
    }else if (self.type == BaseTypeYaRent){
        
        //押金方式
        NSArray *arr = @[@"1",@"2",@"3"];
        self.BTNModel.deposit_method = arr[tag];
        
        
    }else if(self.type == BaseTypeShopLeiXing){
        
        self.BTNModel.shopLeiXing = self.BTNModel.btnNameMut[tag];
        
        
    }else if (self.type == BaseTypeOffBuildingLeiXing){
        
        self.BTNModel.offBuildingLeiXing = self.BTNModel.btnNameMut[tag];
        
    }
    
    
    
}



-(CGFloat)caculateStringWidth:(NSString *)str{
    NSDictionary *attrs = @{NSFontAttributeName : MBFont(14)};
    CGSize maxSize = CGSizeMake(CGFLOAT_MAX, 32);
    return [str boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size.width+ 40;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
